/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmAdicionarCliente.java
 *
 * Created on 05/09/2012, 14:41:50
 */
package br.com.petshop.dao.frames;

import br.com.petshop.dao.ClienteDAO;
import br.com.petshop.entidades.Cliente;
import javax.swing.JOptionPane;

/**
 *
 * @author Alexandre
 */
public class frmAtualizarCliente extends javax.swing.JDialog {

    private final ClienteDAO cliDAO;
    private Cliente clienteInEdit;

    /** Creates new form frmAdicionarCliente */
    public frmAtualizarCliente(java.awt.Frame parent, ClienteDAO cliDAO, boolean modal, Cliente clienteInEdit) {
        super(parent, modal);
        this.cliDAO = cliDAO;
        this.clienteInEdit = clienteInEdit;
        initComponents();
        initMyComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jl_Nome = new javax.swing.JLabel();
        jl_Idade = new javax.swing.JLabel();
        jl_Endereco = new javax.swing.JLabel();
        jl_Telefone = new javax.swing.JLabel();
        jl_Cep = new javax.swing.JLabel();
        jl_Cidade = new javax.swing.JLabel();
        jl_Uf = new javax.swing.JLabel();
        tf_Nome = new javax.swing.JTextField();
        tf_Idade = new javax.swing.JTextField();
        tf_Endereco = new javax.swing.JTextField();
        tf_Telefone = new javax.swing.JTextField();
        tf_Cep = new javax.swing.JTextField();
        tf_Cidade = new javax.swing.JTextField();
        tf_Uf = new javax.swing.JTextField();
        jb_Atualizar = new javax.swing.JButton();
        jb_Cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Adicionar Cliente");
        setBackground(java.awt.Color.white);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jl_Nome.setText("Nome: ");

        jl_Idade.setText("Idade: ");

        jl_Endereco.setText("Endereco: ");

        jl_Telefone.setText("Telefone: ");

        jl_Cep.setText("CEP: ");

        jl_Cidade.setText("Cidade: ");

        jl_Uf.setText("UF: ");

        jb_Atualizar.setText("Atualizar");
        jb_Atualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_AtualizarActionPerformed(evt);
            }
        });

        jb_Cancelar.setText("Cancelar");
        jb_Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_CancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jl_Nome)
                            .addComponent(jl_Endereco))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_Endereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tf_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, 329, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jl_Idade)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tf_Idade, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jl_Telefone)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tf_Telefone, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                        .addContainerGap(57, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jl_Cidade)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tf_Cidade, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jl_Uf)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tf_Uf, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(32, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jl_Cep)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tf_Cep, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(jb_Atualizar)
                        .addGap(18, 18, 18)
                        .addComponent(jb_Cancelar)
                        .addContainerGap(62, Short.MAX_VALUE))))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jl_Cep, jl_Cidade, jl_Endereco, jl_Idade, jl_Nome, jl_Telefone, jl_Uf});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {tf_Endereco, tf_Nome});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_Nome)
                    .addComponent(tf_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_Endereco)
                    .addComponent(tf_Endereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_Idade)
                    .addComponent(tf_Idade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Telefone)
                    .addComponent(tf_Telefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jl_Cidade)
                    .addComponent(tf_Cidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Uf)
                    .addComponent(tf_Uf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_Cep, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jl_Cep)
                    .addComponent(jb_Atualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_Cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 411, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-456)/2, (screenSize.height-293)/2, 456, 293);
    }// </editor-fold>//GEN-END:initComponents

    private void jb_AtualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_AtualizarActionPerformed

        int result = JOptionPane.showConfirmDialog(this, "Confirma?", "Atualizacao de Cliente", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (result != 0) {
            return;
        }
        try {
            clienteInEdit.setNome(tf_Nome.getText());
            clienteInEdit.setIdade(Integer.parseInt(tf_Idade.getText()));
            clienteInEdit.setEndereco(tf_Endereco.getText());
            clienteInEdit.setTelefone(tf_Telefone.getText());
            clienteInEdit.setCep(tf_Cep.getText());
            clienteInEdit.setCidade(tf_Cidade.getText());
            clienteInEdit.setUf(tf_Uf.getText());
            cliDAO.atualizarCliente(clienteInEdit);
            setVisible(false);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Erro ao atualizar Cliente");
            return;
        }
    }//GEN-LAST:event_jb_AtualizarActionPerformed

    private void jb_CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_CancelarActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jb_CancelarActionPerformed
    /**
     * @param args the command line arguments
    
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    
    public void run() {
    frmAdicionarCliente dialog = new frmAdicionarCliente(new javax.swing.JFrame(), true);
    dialog.addWindowListener(new java.awt.event.WindowAdapter() {
    
    public void windowClosing(java.awt.event.WindowEvent e) {
    System.exit(0);
    }
    });
    dialog.setVisible(true);
    }
    });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jb_Atualizar;
    private javax.swing.JButton jb_Cancelar;
    private javax.swing.JLabel jl_Cep;
    private javax.swing.JLabel jl_Cidade;
    private javax.swing.JLabel jl_Endereco;
    private javax.swing.JLabel jl_Idade;
    private javax.swing.JLabel jl_Nome;
    private javax.swing.JLabel jl_Telefone;
    private javax.swing.JLabel jl_Uf;
    private javax.swing.JTextField tf_Cep;
    private javax.swing.JTextField tf_Cidade;
    private javax.swing.JTextField tf_Endereco;
    private javax.swing.JTextField tf_Idade;
    private javax.swing.JTextField tf_Nome;
    private javax.swing.JTextField tf_Telefone;
    private javax.swing.JTextField tf_Uf;
    // End of variables declaration//GEN-END:variables

    private void initMyComponents() {
        try {
            tf_Nome.setText(clienteInEdit.getNome());
            tf_Idade.setText(""+clienteInEdit.getIdade());
            tf_Endereco.setText(clienteInEdit.getEndereco());
            tf_Telefone.setText(clienteInEdit.getTelefone());
            tf_Cidade.setText(clienteInEdit.getCidade());
            tf_Uf.setText(clienteInEdit.getUf());
            tf_Cep.setText(clienteInEdit.getCep());
            tf_Nome.requestFocus();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
